package com.mark.demo.service;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <h1>
 *
 * </h1>
 *
 * @author MarkerCCC
 * @date 2020/4/27 - 16:59
 * @since 2020/4/27 - 16:59 星期一 by MarkerCCC markerccc@163.com
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MarkUserDetailsServiceImpl implements MarkUserDetailsService {

    // private final RemoteUserService remoteUserService;
    private final CacheManager cacheManager;

    /**
     * 用户密码登录
     *
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    @SneakyThrows
    public UserDetails loadUserByUsername(String username) {
        Cache cache = cacheManager.getCache("user_details");
        if (cache != null && cache.get(username) != null) {
            return (MarkCCCUser) cache.get(username).get();
        }

        // R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
        Set<String> dbAuthsSet = new HashSet<>();
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
        // UserDetails userDetails = getUserDetails(result);
        // cache.put(username, userDetails);
        // return userDetails;

        return new MarkCCCUser(1, 1, "15072146145", "1", 1, "markerccc", "{noop}123456", true, true, true, true, authorities);
        // return new PigxUser("1", 1, "1", username, "1", true, true, true, true, authorities);
    }

    @Override
    @SneakyThrows
    public UserDetails loadUserBySocial(String code) {
        Set<String> dbAuthsSet = new HashSet<>();
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
        return new MarkCCCUser(1, 1, "15072146145", "1", 1, "markerccc", "{noop}123456", true, true, true, true, authorities);
    }

    /**
     * 根据社交登录code 登录
     *
     * @param inStr TYPE@CODE
     * @return UserDetails
     * @throws UsernameNotFoundException
     */
    // @Override
    // @SneakyThrows
    // public UserDetails loadUserBySocial(String inStr) {
    // 	return getUserDetails(remoteUserService.social(inStr, SecurityConstants.FROM_IN));
    // }

    /**
     * 构建userdetails
     *
     * @param result 用户信息
     * @return
     */
    // private UserDetails getUserDetails(R<UserInfo> result) {
    // 	if (result == null || result.getData() == null) {
    // 		throw new UsernameNotFoundException("用户不存在");
    // 	}
    //
    // 	UserInfo info = result.getData();
    // 	Set<String> dbAuthsSet = new HashSet<>();
    // 	if (ArrayUtil.isNotEmpty(info.getRoles())) {
    // 		// 获取角色
    // 		Arrays.stream(info.getRoles()).forEach(roleId -> dbAuthsSet.add(SecurityConstants.ROLE + roleId));
    // 		// 获取资源
    // 		dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
    //
    // 	}
    // 	Collection<? extends GrantedAuthority> authorities
    // 			= AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
    // 	SysUser user = info.getSysUser();
    // 	boolean enabled = StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL);
    // 	// 构造security用户
    //
    // 	return new PigxUser(user.getUserId(), user.getDeptId(), user.getPhone(), user.getAvatar(), user.getTenantId()
    // 			, user.getUsername(), SecurityConstants.BCRYPT + user.getPassword()
    // 			, enabled, true, true
    // 			, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
    // }
}
